package com.stx.bms.web;

import com.stx.bms.dao.UserDAO;
import com.stx.bms.dao.UserDAOImpl;
import com.stx.bms.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String username = request.getParameter("uname");
        String password = request.getParameter("pass");

        UserDAO dao = new UserDAOImpl();
        User user = dao.login(username, password);

        if(user == null){
            //用户名或密码错误
            request.setAttribute("errMsg", "用户名或密码错误");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }else{
            //登录成功，保存当前用户信息(HttpSession)
            HttpSession session = request.getSession();
            session.setAttribute("loginUser", user);
            // 进入首页
            response.sendRedirect("admin/home.jsp");
        }
    }
}
